#include "linklist.h"
linklist list;

int main(void)
{
    int a;
    InitList(); // 初始化头节点
    linklist head = list;
    linklist tail = list;
    // 使用头插法
    HeadInsertElem(&head, 1);
    // printf("main list->next %p\n", list->next);
    HeadInsertElem(&head, 2);
    HeadInsertElem(&head, 3);

    InsertElem(&list, 3, 7);


    /* while (head->next != NULL)
    {
        printf("%d\n", (head->next)->data);
        head = head->next;
    } */
    printlist(list);
    int len = ListLength(list);
    printf("init length = %d\n", ListLength(list));
    GetElem(list, 3, &a);
    printf("at 3 is = %d\n", a);
    printf("Search 1 at list %d\n", SearchElem(list, 1));
    DeleteElem(&list, 4, &a);
    printf("Delete elem is %d\n", a);
    /* head = list;
    while (head->next != NULL)
    {
        printf("%d\n", (head->next)->data);
        head = head->next;
    } */
    printlist(list);




    ClearList(&list);
    // printf("main： head -> %p, tail -> %p\n", head, tail);
    /* TailInsertElem(&tail, 4);
    TailInsertElem(&tail, 5);
    TailInsertElem(&tail, 6);
    while (head->next != NULL)
    {
        printf("%d\n", (head->next)->data);
        head = head->next;
    } */
    
    int re = isEmpty(&list);
    printf("empty is %d\n", re);
    // printf("%d\n", head->next);
    
    return 0;
}

void InitList()
{
    list = (linklist) malloc(sizeof(struct Node));
    if (list == NULL)
    {
        exit(-1);
    }
    
    list->data = 0;
    list->next = NULL;
}

int isEmpty(linklist* list)
{
    if ((*list)->next != NULL) return 0; // no empty
    return 1; // empty 
}

void ClearList(linklist* list)
{
    linklist p, q;
    p = (*list)->next;
    while(p != NULL)
    {
        q = p->next;
        free(p);
        p = q;
    }
    (*list)->next = NULL;
}

int ListLength(linklist list)
{
    int i = 0;
    linklist p = list->next;
    while (p != NULL)
    {
        i ++;
        p = p->next;
    }
    return i;
    printf("list->next %p\n", p);
}

void GetElem(linklist list, int i, int* tmp)
{
    int j;
	linklist p;		/* 声明一结点p */
	p = list->next;		/* 让p指向链表L的第一个结点 */
	j = 1;		/*  j为计数器 */
	while (p && j < i)  /* p不为空或者计数器j还没有等于i时，循环继续 */
	{   
		p = p->next;  /* 让p指向下一个结点 */
		++j;
	}
	if ( !p || j > i ) 
		exit(-1);  /*  第i个元素不存在 */
	*tmp = p->data;   /*  取第i个元素的数据 */
}

int SearchElem(linklist list, int tmp)
{
    int i = 0;
    linklist p = list->next;
    while (p != NULL)
    {
        i ++;
        if (tmp == p->data) return i;
        p = p->next;
    }
    return -1;
}

int InsertElem(linklist* list, int i, int data)
{
    linklist tmp;
    linklist p = (*list);
    int j = 1;
    while (p != NULL && j < i)
    {
        j ++;
        p = p->next;
    }
    if (p == NULL && j > i) return -1;
    // p 是前一个结点
    tmp = (linklist) malloc(sizeof(struct Node));
    tmp->data = data;
    tmp->next = p->next;
    p->next = tmp;
    return 1;
}

int DeleteElem(linklist* list, int i, int* data)
{
    linklist tmp; // 被删除节点
    linklist p = (*list); // 被删除节点的前一个
    int j = 1;
    while (p->next != NULL && j < i)
    {
        j ++;
        p = p->next;
    }
    if (p->next == NULL && j > i) return -1;
    // p 是前一个结点
    tmp = p->next;
    *data = tmp->data;
    p->next = tmp->next;
    free(tmp);
    return 1;
}

void visit(int* data)
{
    printf("%d ", *data);
}

void printlist(linklist list)
{
    linklist p = list->next;
    while (p != NULL)
    {
        visit(&(p->data));
        p = p->next;
    }
    putchar('\n');
}





// 头插法
void HeadInsertElem(linklist* head, int data)
{
    linklist tmp = (linklist) malloc(sizeof(Node));
    // 设置数据
    tmp->data = data;
    tmp->next = list->next;
    (*head)->next = tmp;
    
}

// 尾插法
void TailInsertElem(linklist* tail, int data)
{
    // (*tail) 相当于 main 里的 tail
    // 如果不传入指针，则是传值
    // printf("old_tail -> %p, ", *tail);
    linklist tmp = (linklist) malloc(sizeof(struct Node));
    (*tail)->next = tmp;
    tmp->data = data;
    tmp->next = NULL;
    *tail = tmp;
    
    // printf("tail -> %p, tmp -> %p\n", *tail, tmp);
}